﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AlmaDatabase;
using Alma.Models;
using System.Diagnostics;

namespace Alma.Controllers
{
    public class ComparatorController : Controller
    {
        //
        // GET: /Comparator/

        public ActionResult Edit()
        {
            ComparatorModel newComp = new ComparatorModel();
            using (var context = new AlmaDBEntities())
            {
                var pro = from products in context.Products select products;
                /* foreach (Product p in pro)
                 {
                     list.Add(p.Name);
                     list2.Add(p.Name);
                 }*/
                if (Session["comparator"] != null)
                    foreach (int id in (List<int>)Session["comparator"])
                    {
                        pro = from products in context.Products where products.ID == id select products;
                        newComp.productList.Add(new ProductDetailsModel(pro.First()));
                    }
                if (newComp.product1 != null)
                {
                    pro = from products in context.Products where products.Name == newComp.product1 select products;
                    newComp.productList.Add(new ProductDetailsModel(pro.First()));
                }
                if (newComp.product2 != null)
                {
                    pro = from products in context.Products where products.Name == newComp.product2 select products;
                    newComp.productList.Add(new ProductDetailsModel(pro.First()));
                }
                newComp.BuildCriterions();
            }
            /*   ViewData["drop1"] = new SelectList(list);
               ViewData["drop2"] = new SelectList(list2);*/
            return View(newComp);
        }

        //[HttpPost]
        //public ActionResult Edit(ComparatorModel newComp)
        //{

        // /*   List<string> list = new List<string>();
        //    List<string> list2 = new List<string>();*/
        //    using (var context = new AlmaDBEntities())
        //    {
        //        var pro = from products in context.Products select products;
        //       /* foreach (Product p in pro)
        //        {
        //            list.Add(p.Name);
        //            list2.Add(p.Name);
        //        }*/
        //        if (Session["comparator"] != null)
        //            foreach (int id in (List<int>)Session["comparator"])
        //        {
        //            pro = from products in context.Products where products.ID == id select products;
        //            newComp.productList.Add(new ProductDetailsModel(pro.First()));
        //        }
        //        if (newComp.product1 != null)
        //        {
        //            pro = from products in context.Products where products.Name == newComp.product1 select products;
        //            newComp.productList.Add(new ProductDetailsModel(pro.First()));
        //        }
        //        if (newComp.product2 != null)
        //        {
        //            pro = from products in context.Products where products.Name == newComp.product2 select products;
        //            newComp.productList.Add(new ProductDetailsModel(pro.First()));
        //        }

        //    }
        // /*   ViewData["drop1"] = new SelectList(list);
        //    ViewData["drop2"] = new SelectList(list2);*/
        //    return View(newComp);
        //}

        public ActionResult Add(int Id)
        {
            List<int> tab = (List<int>)Session["comparator"];
            if (tab == null)
                tab = new List<int>();
            else
            foreach(int id in tab)
                if (id == Id)
                    return RedirectToAction("Edit", "Comparator");
            tab.Add(Id);
            Session["comparator"] = tab;
            return RedirectToAction("Edit", "Comparator");
        }

        public ActionResult Delete(int Id)
        {
            List<int> tab = (List<int>)Session["comparator"];
            tab.Remove(Id);
            Session["comparator"] = tab;
            return RedirectToAction("Edit", "Comparator");
        }

        public ActionResult Free()
        {
            Session["comparator"] = new List<int>();
            return RedirectToAction("Edit", "Comparator");
        }

    }
}
